/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.SABM;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import entity.*;
import java.util.ArrayList;
import session.TX.*;
import managedBean.TX.CustomerIOTRPortal.FakeMainPortalMB;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

/**
 *
 * @author Randall Chia
 */
@ManagedBean(name = "rewardMB")
@SessionScoped
public class RewardMB {
    
    @EJB
    private ULPSBRemote pointsManager;


    //data fields for making a new reward item
    private String stakeholder; //Used for choosing a stakeholder
    private String customerEmail;
    private String reward_Name;
    private String reward_Description;
    private Integer reward_Cost;         //the number of points needed to redeem this reward
    private Integer vouchers_left;       //the maximum number of times this reward can be redeemed. 
    private boolean unlimitedVouchers;
    private Integer loyaltyPoint;
    
    /** Creates a new instance of RewardMB */
    public RewardMB() { 
    }
     
    //STAKEHOLDER-LEVEL FUNCTIONS
    //=========================================================================================================================
    public List<RewardItem> getStakeholderItems(){
      stakeholder = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderName").toString();
      return getPointsManager().getRewardsByStakeholder(stakeholder);
    } 
    
    public void deleteReward(RewardItem itemToDelete){
        System.out.println(">>> Deleting a reward item");
        getPointsManager().stakeholderDeleteRewardItem(itemToDelete.getId());
        System.out.println(">>> Done.");
    }
    
    public void createReward(){
        System.out.println(">>> Creating a reward item");
        String stakeholderName =  FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderName").toString();
        
        getPointsManager().stakeholderCreateRewardItem(stakeholderName, reward_Name, reward_Description, reward_Cost, vouchers_left, unlimitedVouchers);
        System.out.println(">>> Done.");
        reward_Name = null;
        reward_Description = null;
        reward_Cost = null;
        vouchers_left = null;
    }
    //=========================================================================================================================
    
    
    //OUTLET-LEVEL FUNCTIONS
    //=========================================================================================================================
    public List<RewardVoucher> getCustomerVouchers(){
        List<RewardVoucher> returnList = new ArrayList<RewardVoucher>();
        stakeholder = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("outletOwner").toString();
        
        //Outlet managers can only get vouchers of rewards from their stakeholder. Only non-redeemed vouchers are shown.
        for(RewardVoucher voucher : pointsManager.getVouchersByCustomerAndStakeholder(customerEmail, stakeholder)){
            if(voucher.isRedeemed() == false) returnList.add(voucher);
        }
        
        return returnList;
        
    }
    
    public void redeemVoucher(long voucherID){
        System.out.println("MANAGED BEAN: Redeeming voucher...");
        pointsManager.redeemVoucher(voucherID);
        System.out.println("MANAGED BEAN: Finished...");
    }
    //=========================================================================================================================
    
    
    //USER-LEVEL FUNCTIONS
    //=========================================================================================================================
    public List<RewardVoucher> getVouchersByCustomer(){
        List<RewardVoucher> returnList = new ArrayList<RewardVoucher>();
        
        long customerID = (Long)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("accountId");
        
        for(RewardVoucher voucher : pointsManager.getVouchersByCustomer(customerID)){
            returnList.add(voucher);
        }      
        return returnList;  
    }
    
    
    public List<RewardItem> getAllRewards(){
        return pointsManager.getAllRewards();
    }
    
    
    public Integer getUserLoyaltyPoints(){
        String email = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("email").toString();   
        return pointsManager.getUserLoyaltyPoints(email);
    }
    
    
    public String getPointsStatus(){
        String email = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("email").toString();
        
        String status = pointsManager.getUserPointsExpiryStatus(email);
        String[] array = status.split(" ");
        Integer customerPoints = getUserLoyaltyPoints();
        
        status = "You currently have <b>" + customerPoints + " points</b>.<br/>"
                +"Your last item purchase transaction was on " + array[0] + "<br/>"
                +"Your points will expire on: " + array[1];
        return status;       
    }
    
    
    public void buyRewardVoucher(long id){
        String email = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("email").toString();
        //System.out.println(">>> CUSTOMER BUY VOUCHER");
        String msg = pointsManager.buyVoucher(email, id);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Voucher Purchase", msg));
    }
    
    //This method is used to return a boolean whether or not a customer can afford a certain reward.
    //The boolean is used by the JSF page to determine whether or not to Disable the buy button for the reward.
    public boolean cannotAfford(Integer rewardCost){
        Integer customerPoints = getUserLoyaltyPoints();
        if(rewardCost > customerPoints) return false;
        else return true;  
    }
    
    public void setStakeholderLoyaltyPoint() {
        String statusMessage;
        boolean result = pointsManager.setStakeholderLoyaltyPoints(stakeholder, getLoyaltyPoint());
        if(result == true){
            statusMessage = "Loyalty points changes successfully";
        }else{
            statusMessage = "Loyalty points change unsuccessful";
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + statusMessage, ""));
    }
    
    public void setUserLoyaltyPoint() {
        String statusMessage;
        boolean result;      
        result = pointsManager.setUserLoyaltyPoints(customerEmail, getLoyaltyPoint());
        if(result == true){
            statusMessage = "Loyalty points changes successfully";
        }else{
            statusMessage = "Loyalty points change unsuccessful";
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + statusMessage, ""));
    }
    
    public Stakeholder searchStakeholder() {
        return pointsManager.findStakeholderByName(stakeholder);
    }
    
    public Users searchUser() {
        return pointsManager.findCustomer(customerEmail);
    }
    

    /**
     * @return the pointsManager
     */
    public ULPSBRemote getPointsManager() {
        return pointsManager;
    }

    /**
     * @param pointsManager the pointsManager to set
     */
    public void setPointsManager(ULPSBRemote pointsManager) {
        this.pointsManager = pointsManager;
    }

    /**
     * @return the stakeholder
     */
    public String getStakeholder() {
        return stakeholder;
    }

    /**
     * @param stakeholder the stakeholder to set
     */
    public void setStakeholder(String stakeholder) {
        this.stakeholder = stakeholder;
    }

    /**
     * @return the reward_Name
     */
    public String getReward_Name() {
        return reward_Name;
    }

    /**
     * @param reward_Name the reward_Name to set
     */
    public void setReward_Name(String reward_Name) {
        this.reward_Name = reward_Name;
    }

    /**
     * @return the reward_Description
     */
    public String getReward_Description() {
        return reward_Description;
    }

    /**
     * @param reward_Description the reward_Description to set
     */
    public void setReward_Description(String reward_Description) {
        this.reward_Description = reward_Description;
    }

    /**
     * @return the reward_Cost
     */
    public Integer getReward_Cost() {
        return reward_Cost;
    }

    /**
     * @param reward_Cost the reward_Cost to set
     */
    public void setReward_Cost(Integer reward_Cost) {
        this.reward_Cost = reward_Cost;
    }

    /**
     * @return the vouchers_left
     */
    public Integer getVouchers_left() {
        return vouchers_left;
    }

    /**
     * @param vouchers_left the vouchers_left to set
     */
    public void setVouchers_left(Integer vouchers_left) {
        this.vouchers_left = vouchers_left;
    }

    /**
     * @return the unlimitedVouchers
     */
    public boolean isUnlimitedVouchers() {
        return unlimitedVouchers;
    }

    /**
     * @param unlimitedVouchers the unlimitedVouchers to set
     */
    public void setUnlimitedVouchers(boolean unlimitedVouchers) {
        this.unlimitedVouchers = unlimitedVouchers;
    }

    /**
     * @return the customer
     */
    public String getCustomerEmail() {
        return customerEmail;
    }

    /**
     * @param customer the customer to set
     */
    public void setCustomerEmail(String customerEmail) {
        this.customerEmail = customerEmail;
    }

    /**
     * @return the loyaltyPoint
     */
    public Integer getLoyaltyPoint() {
        return loyaltyPoint;
    }

    /**
     * @param loyaltyPoint the loyaltyPoint to set
     */
    public void setLoyaltyPoint(Integer loyaltyPoint) {
        this.loyaltyPoint = loyaltyPoint;
    }
}
